import 'package:flutter/material.dart';

import '../../constant/colors.dart';

/// 页面 loading 占位。
class LoadingPlaceholder extends StatelessWidget {
  const LoadingPlaceholder({
    super.key,
    this.width = 30,
    this.height = 30,
    this.alignment = Alignment.center,
    this.yOffset,
    this.color,
  });

  final double width;
  final double height;
  final AlignmentGeometry? alignment;
  final double? yOffset;
  final Color? color;

  @override
  Widget build(BuildContext context) {
    final child = alignment != null
        ? Align(
            alignment: alignment!,
            child: loadingWidget,
          )
        : loadingWidget;

    final offset = yOffset;
    if (offset == null || offset == 0) {
      return child;
    }

    return Padding(
      padding: EdgeInsets.only(
        top: offset > 0 ? offset : 0,
        bottom: offset < 0 ? -offset : 0,
      ),
      child: child,
    );
  }

  Widget get loadingWidget => SizedBox(
        width: width,
        height: height,
        child: CircularProgressIndicator(
          color: color ?? MagicColors.themeTitle,
        ),
      );
}
